package com.luyue.zagj.activity.myAccount;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luyue.zagj.R;
import com.luyue.zagj.activity.base.LoginActivity;
import com.luyue.zagj.application.MyApl;
import com.luyue.zagj.utils.SPutils;

import java.io.FileNotFoundException;
import java.io.InputStream;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetActivity extends AppCompatActivity {

    private static final int REQUEST_ALBUM_OK = 1;
    private static final String TAG = "Setactivity";
    @Bind(R.id.set_cancel_btn)
    Button setCancelBtn;
    @Bind(R.id.set_top_rl)
    RelativeLayout setTopRl;
    @Bind(R.id.set_portrait_btn)
    ImageView setPortraitBtn;
    @Bind(R.id.set_setimg_tv)
    TextView setSetimgTv;
    @Bind(R.id.set_portrait_fm)
    FrameLayout setPortraitFm;
    @Bind(R.id.set_change_phone_rl)
    RelativeLayout setChangePhoneRl;
    @Bind(R.id.set_msg_rl)
    RelativeLayout setMsgRl;
    @Bind(R.id.set_feedback_rl)
    RelativeLayout setFeedbackRl;
    @Bind(R.id.activity_set)
    LinearLayout activitySet;
    @Bind(R.id.back_app)
    TextView backApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.set_cancel_btn, R.id.set_change_phone_rl, R.id.set_msg_rl, R.id.set_feedback_rl,R.id.back_app,R.id.set_portrait_btn})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.set_cancel_btn:
                onBackPressed();
                break;
            case R.id.set_change_phone_rl:
                Intent intent = new Intent(this, UpdatePhoneActivity.class);
                startActivity(intent);
                //
                break;
            case R.id.set_portrait_btn:
                showImage();
                break;
            case R.id.set_msg_rl:
                setSkipActivity(MessageACtivity.class);
                break;
            case R.id.set_feedback_rl:
                setSkipActivity(FeedBackActivity.class);
                break;
            case R.id.back_app:
                SPutils.put(SetActivity.this, MyApl.FIRST_OPEN, false);
                Intent intent3 = new Intent(this, LoginActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent3);
                break;
        }
    }

    private void setSkipActivity(Class activity) {
        Intent intent1=new Intent(this,activity);
        startActivity(intent1);
    }

    private void showImage() {
            Intent albumIntent = new Intent(Intent.ACTION_PICK, null);
            albumIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(albumIntent, REQUEST_ALBUM_OK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode){
            case REQUEST_ALBUM_OK:

                Log.d(TAG, "onActivityResult:相册 " + data.getData().toString());
                ContentResolver resolver = getContentResolver();
                try {
                    InputStream inputStream = resolver.openInputStream(data.getData());
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    setPortraitBtn.setBackgroundDrawable(new BitmapDrawable(bitmap));

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            break;
        }
    }
}
